package ran.youling.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


/***
 * NAME:Ran
 * TIME:2025/1/2
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启安全认证
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //    数据库密码加密解密的bean
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    //    重写权限认证的方法
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //    访问限制
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()//关闭csrf攻击
                .authorizeHttpRequests().antMatchers("/login", "/enroll", "/index")//放行接口
                .authenticated();//其它接口需验证
    }

}
